var express = require("express");
var router = express.Router();
const usersService = require("../service/usesService");
/* GET users listing. */

//获取所有的 users
router.get("/", async function (req, res) {
  let { current, size } = req.query;
  console.log(req.query, "query");
  let user = {};
  const data = await usersService.find(user, {
    current: current || 1,
    size: size || 5,
  });
  res.send(data);
});
//通过 id 获取
router.get("/:id", async function (req, res) {
  let id = req.params.id;
  let data = await usersService.findById(id);
  res.send(data);
});

// 添加用户
router.post("/", async function (req, res, next) {
  let { username } = req.body;
  // 查询用户是否存在
  let data = await usersService.findOneId(username);
  if (data == true) {
    // true 新增
    let data = await usersService.insert(req.body);
    res.send(data);
  } else {
    res.send(data);
  }
});

//修改用户

router.put("/:id", async function (req, res) {
  let id = req.params.id;
  let data = await usersService.update(id, req.body);
  res.send(data);
});

//删除用户
router.delete("/:id", async function (req, res) {
  let id = req.params.id;
  let data = await usersService.delete(id);
  res.send(data);
});

module.exports = router;
